package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.logistic.LogisticEditStatusEnum;
import com.linkhub.oms.dao.mapper.LogisticEntityMapper;
import com.linkhub.oms.dao.mpservice.ILogisticService;
import com.linkhub.oms.entity.LogisticEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 物流渠道表 服务实现类
 * </p>
 *
 * @author Jaycle
 * @version 1.0.0
 * @Description LogisticServiceImpl.java
 * @createTime 2024/04/29 23:17
 */
@Slf4j
@Service
public class LogisticServiceImpl extends ServiceImpl<LogisticEntityMapper, LogisticEntity> implements ILogisticService {

    @PostConstruct
    @Scheduled(cron = "0 0 0 * * ?")
    @Override
    public void initLogisticsStatus() {
        log.info("Initialize the logistic status start...");
        List<LogisticEntity> logisticEntityList = lambdaQuery().list();
        for (LogisticEntity logisticEntity : logisticEntityList) {
            LocalDate startDownTime = logisticEntity.getStartDownTime();
            LocalDate endDownTime = logisticEntity.getEndDownTime();
            LocalDate nowDate = LocalDate.now(ZoneId.of("Asia/Shanghai"));
            String channelCode = logisticEntity.getChannelCode();
            // 仅当前日期在停用范围内或停止停用时间后需处理
            if ((startDownTime != null && nowDate.compareTo(startDownTime) >= 0) && (endDownTime == null || nowDate.compareTo(endDownTime) < 0)) {
                disableLogistic(channelCode, LogisticEditStatusEnum.DISABLE, startDownTime, endDownTime);
            } else if (endDownTime != null && nowDate.compareTo(endDownTime) >= 0) {
                enableLogistic(channelCode);
            }
        }
        log.info("Initialize the logistic status end...");
    }

    /**
     * 删除物流渠道优先级接口
     *
     * @param sysShopId    sysShopId
     * @param shippingName shippingName
     */
    @Override
    public void deleteChannelPriority(String sysShopId, String shippingName) {

    }

    @Override
    public List<LogisticEntity> listByChannelCodes(Collection<String> channelCodes) {

        if (CollUtil.isEmpty(channelCodes)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return lambdaQuery()
                .in(LogisticEntity::getChannelCode, channelCodes)
                .list();
    }

    @Override
    public List<LogisticEntity> listEnableChannels() {
        return super.lambdaQuery()
                .eq(LogisticEntity::getChannelState, LogisticEditStatusEnum.ENABLE)
                .list();
    }

    /**
     * @param channelState
     * @param keyWord
     * @return
     */
    @Override
    public List<LogisticEntity> listLogistics(Integer channelState, String keyWord) {
        // 物流渠道状态
        boolean hasChannelState = Objects.nonNull(channelState);
        // 物流名称或物流渠道名称，支持同时搜索两个字段
        boolean hasLogisticName = StringUtils.isNotBlank(keyWord);
        return lambdaQuery()
                .eq(hasChannelState, LogisticEntity::getChannelState, channelState)
                .and(hasLogisticName, wrapper -> wrapper
                        .like(LogisticEntity::getLogisticsName, keyWord)
                        .or()
                        .like(LogisticEntity::getChannelEName, keyWord)
                        .or()
                        .like(LogisticEntity::getChannelCName, keyWord)
                )
                .orderByAsc(LogisticEntity::getLogisticsName)
                .orderByDesc(LogisticEntity::getChannelState)
                .orderByAsc(LogisticEntity::getChannelEName)
                .list();
    }

    /**
     * @param channelCode
     */
    @Override
    public void enableLogistic(String channelCode) {
        LogisticEntity logisticEntity = new LogisticEntity();
        logisticEntity.setChannelState(LogisticEditStatusEnum.ENABLE);

        lambdaUpdate()
                .eq(LogisticEntity::getChannelCode, channelCode)
                .set(LogisticEntity::getStartDownTime, null)
                .set(LogisticEntity::getEndDownTime, null)
                .update(logisticEntity);
    }

    /**
     * @param channelCode
     * @param logisticEditStatusEnum
     * @param startDate
     * @param endDate
     */
    @Override
    public void disableLogistic(String channelCode, LogisticEditStatusEnum logisticEditStatusEnum, LocalDate startDate, LocalDate endDate) {
        LogisticEntity logisticEntity = new LogisticEntity();
        logisticEntity.setChannelState(logisticEditStatusEnum);
        logisticEntity.setStartDownTime(startDate);
        logisticEntity.setEndDownTime(endDate);
        lambdaUpdate()
                .eq(LogisticEntity::getChannelCode, channelCode)
                .set(endDate == null, LogisticEntity::getEndDownTime, null)
                .update(logisticEntity);
    }

}
